RDF Terms
This package contains utility functions for handling
RDFJS terms of quads/triples.
Usage
The following examples assume the following imports:
import { DataFactory } from "rdf-data-factory";
import * as RdfTerms from "rdf-terms";
const factory = new DataFactory();
Constants
console.log(RdfTerms.QUAD_TERM_NAMES);
console.log(RdfTerms.TRIPLE_TERM_NAMES);
console.log(RdfTerms.TERM_TYPES);
Get quad terms
Get all terms from a quad.
A second optional parameter can be set to true to ignore graph terms in the default graph.
RdfTerms.getTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
));
RdfTerms.getTerms(factory.triple(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
));
RdfTerms.getTerms(factory.triple(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
), true);
Get quad nested terms
Get all nested terms from a quad.
This means that if a quad's term is a nested term, instead of returning that quad, all nested terms will be included, recursively.
A second optional parameter can be set to true to ignore graph terms in the default graph.
RdfTerms.getTerms(factory.quad(
factory.quad(
namedNode('http://example.org/a'),
namedNode('http://example.org/b'),
namedNode('http://example.org/c'),
namedNode('http://example.org/d'),
),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
));
Get named quad terms
Get all terms from a quad labelled with the quad term name.
This is the reverse operation from collectNamedTerms
.
RdfTerms.getNamedTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
));
Collect named quad terms
Create a quad from a an array of named quad terms.
This is the reverse operation from getNamedTerms
.
An second optional callback method can be provided to fill in missing terms
RdfTerms.collectNamedTerms([
{ subject: factory.namedNode('http://example.org/s') },
{ predicate: factory.namedNode('http://example.org/p') },
{ object: factory.literal('abc') },
{ graph: factory.namedNode('http://example.org/g') },
]);
RdfTerms.collectNamedTerms([
{ subject: factory.namedNode('http://example.org/s') },
{ object: factory.literal('abc') },
{ graph: factory.namedNode('http://example.org/g') },
], (termName) => factory.namedNode('http://example.org/newNode'));
An third optional argument can be passed to set a custom data factory
RdfTerms.collectNamedTerms([
{ subject: factory.namedNode('http://example.org/s') },
{ predicate: factory.namedNode('http://example.org/p') },
{ object: factory.literal('abc') },
{ graph: factory.namedNode('http://example.org/g') },
], null, myDataFactory);
Iterate over quad terms
Invokes a callback for each term in the quad.
RdfTerms.forEachTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), (value, key) => console.log(key + ': ' + value.value));
Iterate over nested quad terms
Invokes a callback for each term in the quad, while recursing into quoted triples.
RdfTerms.forEachTermsNested(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
literal('abc'),
),
namedNode('http://example.org/g'),
), (value, keys) => console.log(keys + ': ' + value.value));
Filter quad terms
Returns all quad terms that return true for a given filter.
RdfTerms.filterTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), (value, key) => key === 'predicate');
Filter nested quad terms
Returns all quad terms that return true for a given filter, while recursing into quoted triples.
RdfTerms.filterTermsNested(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
literal('abc'),
),
namedNode('http://example.org/g'),
), (value, keys) => keys[keys.length - 1] === 'predicate');
Filter quad term names
Returns all quad term names that return true for a given filter.
RdfTerms.filterQuadTermNames(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), (value, key) => value.equals(namedNode('http://example.org/p')));
Filter nested quad term names
Returns all quad term names that return true for a given filter, while recursing into quoted triples.
RdfTerms.filterQuadTermNamesNested(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
literal('abc'),
),
namedNode('http://example.org/g'),
), (value, keys) => value.equals(namedNode('http://example.org/p')));
Map quad terms
Map all quad terms to form a new quad.
RdfTerms.mapTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), (value, key) => namedNode('http://' + key));
An second optional argument can be passed to set a custom data factory
RdfTerms.mapTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
),
(value, key) => namedNode('http://' + key),
myDataFactory);
Map nested quad terms
Map all quad terms to form a new quad, while recursing into quoted triples.
RdfTerms.mapTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
literal('abc'),
),
namedNode('http://example.org/g'),
), (value, keys) => namedNode('http://' + keys.join('-')));
Reduce quad terms
Reduce the quad terms to a single value.
RdfTerms.reduceTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), (previous, value, key) => previous + ', ' + value.value, 'START: ');
Reduce nested quad terms
Reduce the quad terms to a single value, while recursing into quoted triples.
RdfTerms.reduceTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
literal('abc'),
),
namedNode('http://example.org/g'),
), (previous, value, key) => previous + ', ' + value.value, 'START: ');
Every quad terms
Determines whether all terms satisfy the specified test.
RdfTerms.everyTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), (value, key) => value.termType === 'NamedNode');
RdfTerms.everyTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
namedNode('http://example.org/o'),
namedNode('http://example.org/g'),
), (value, key) => value.termType === 'NamedNode');
Every quoted quad terms
Determines whether all terms satisfy the specified test, while recursing into quoted triples.
RdfTerms.everyTermsNested(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
literal('abc'),
),
namedNode('http://example.org/g'),
), (value, keys) => value.termType === 'NamedNode');
RdfTerms.everyTermsNested(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
namedNode('http://example.org/o1'),
),
namedNode('http://example.org/g'),
), (value, keys) => value.termType === 'NamedNode');
Some quad terms
Determines whether at least one term satisfies the specified test.
RdfTerms.someTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), (value, key) => value.termType === 'NamedNode');
RdfTerms.someTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
namedNode('http://example.org/o'),
namedNode('http://example.org/g'),
), (value, key) => value.termType === 'NamedNode');
RdfTerms.someTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
namedNode('http://example.org/o'),
namedNode('http://example.org/g'),
), (value, key) => value.termType === 'Variable');
Some nested quad terms
Determines whether at least one term satisfies the specified test, while recursing into quoted triples.
RdfTerms.someTermsNested(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
literal('abc'),
),
namedNode('http://example.org/g'),
), (value, keys) => value.termType === 'Literal');
RdfTerms.someTermsNested(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
namedNode('http://example.org/o1'),
),
namedNode('http://example.org/g'),
), (value, keys) => value.termType === 'Literal');
Get value in nested path
Get the nested value inside a quoted triple by the given path of quad keys.
RdfTerms.getValueNestedPath(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
factory.quad(
namedNode('http://example.org/s1'),
namedNode('http://example.org/p1'),
literal('abc'),
),
),
namedNode('http://example.org/g'),
), [ 'object', 'object', 'object' ]);
Match term
Determines if the given term matches with the given term.
RdfTerms.matchTerm(
namedNode('http://example.org/s'),
undefined,
);
RdfTerms.matchTerm(
namedNode('http://example.org/s'),
variable('v'),
);
RdfTerms.matchTerm(
namedNode('http://example.org/s'),
namedNode('http://example.org/s'),
);
RdfTerms.matchTerm(
factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
),
factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
variable('o'),
namedNode('http://example.org/g'),
),
);
Match pattern
Determines if the given quad matches with the given quad terms.
RdfTerms.matchPattern(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
),
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
);
RdfTerms.matchPattern(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
),
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
);
RdfTerms.matchPattern(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
),
namedNode('http://example.org/s'),
variable('someVariableP'),
literal('abc'),
);
RdfTerms.matchPattern(factory.quad(
factory.quad(
namedNode('http://example.org/a'),
namedNode('http://example.org/b'),
namedNode('http://example.org/c'),
namedNode('http://example.org/d'),
),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
),
factory.quad(
namedNode('http://example.org/a'),
variable('someVariableP'),
namedNode('http://example.org/c'),
namedNode('http://example.org/d'),
),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
);
RdfTerms.matchPattern(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
),
namedNode('http://example.org/s'),
variable('someVariableP'),
literal('abcdef'),
);
Match pattern complete
Determines if the given quad matches with the given quad pattern (A quad that contains zero or more variables).
RdfTerms.matchPatternComplete(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
));
RdfTerms.matchPatternComplete(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), factory.quad(
namedNode('http://example.org/s'),
variable('varA'),
literal('abc'),
variable('varB'),
));
RdfTerms.matchPatternComplete(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), factory.quad(
namedNode('http://example.org/s'),
variable('varA'),
literal('abcdef'),
variable('varB'),
));
Match pattern, taking into account variable mappings
Determines if the given quad matches with the given quad pattern (A quad that contains zero or more variables),
by taking into account the mappings of the variables.
If the same variable occurs multiple times in the pattern,
then the corresponding terms in the quad must be equal.
RdfTerms.matchPatternMappings(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
));
RdfTerms.matchPatternComplete(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/X'),
literal('abc'),
namedNode('http://example.org/X'),
), factory.quad(
namedNode('http://example.org/s'),
variable('varA'),
literal('abc'),
variable('varA'),
));
RdfTerms.matchPatternComplete(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/X1'),
literal('abc'),
namedNode('http://example.org/X2'),
), factory.quad(
namedNode('http://example.org/s'),
variable('varA'),
literal('abc'),
variable('varA'),
));
There are also the following optional parameters
skipVarMapping
- Don't add variables in the quad to the mappingreturnMappings
- Return the mappings if it is a valid match
RdfTerms.matchPatternMappings(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), { returnMappings: true });
RdfTerms.matchPatternMappings(factory.quad(
variable('s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
), { returnMappings: true });
RdfTerms.matchPatternMappings(factory.quad(
variable('s'),
namedNode('http://example.org/p'),
variable('p'),
namedNode('http://example.org/g'),
), factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
variable('o'),
namedNode('http://example.org/g'),
), { returnMappings: true });
RdfTerms.matchPatternMappings(factory.quad(
variable('s'),
namedNode('http://example.org/p'),
variable('p'),
namedNode('http://example.org/g'),
), factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
variable('o'),
namedNode('http://example.org/g'),
), { returnMappings: true, skipVarMapping: true });
const quadVariables = factory.quad(variable('s'), variable('p'), variable('o'), variable('g'));
RdfTerms.matchPatternMappings(
factory.quad(quadVariables, variable('p'), variable('f'), variable('g')),
factory.quad(quadVariables, variable('p'), variable('o'), variable('g')),
)
RdfTerms.matchPatternMappings(
factory.quad(quadVariables, variable('p'), variable('f'), variable('g')),
factory.quad(quadVariables, variable('p'), variable('o'), variable('g')),
{ skipVarMapping: true }
)
Unique terms
Create an array of unique terms from the given array.
RdfTerms.uniqTerms([
namedNode('http://example.org/s'),
namedNode('http://example.org/s'),
]);
Get terms of type
Find all terms of the given type in the given array.
RdfTerms.getTermsOfType([
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
], 'NamedNode');
The functions getNamedNodes
, getBlankNodes
, getLiterals
, getVariables
, getDefaultGraphs
are convenience variants of this function,
which do not require the term type string as parameter, and perform appropriate casting in TypeScript.
RdfTerms.getNamedNodes([
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
]);
License
This software is written by Ruben Taelman.
This code is released under the MIT license.